<?php


namespace App\Repositories;



use Illuminate\Database\Eloquent\Model;

abstract class BaseRepository {
	/**
	 * The Model instance.
	 *
	 * @var Model
	 */
	protected $model;
	
	
	/**
	 * Get number of records.
	 *
	 * @return array
	 */
	public function getNumber()
	{
		$total = $this->model->query()->count();
		$new = $this->model->query()->whereSeen(0)->count();
		return compact('total', 'new');
	}
	
	/**
	 * @param array $where
	 * @return int
	 */
	public  function getSumCount($where = [])
	{
		return $this->model->query ()->where ($where)->count ('id');
	}
	
	/**
	 * Destroy a model.
	 *
	 * @param  int $id
	 * @return void
	 */
	public function destroy($id)
	{
		$this->getById($id)->delete();
	}
	
	/**
	 * Get Model by id.
	 *
	 * @param $id
	 * @return mixed
	 */
	public function getById($id)
	{
		return $this->model->query()->findOrFail($id);
	}
	
	/**
	 * @param array $where
	 * @param array $content
	 * @return \Illuminate\Database\Eloquent\Builder|Model
	 */
	public function getFirst($where = [],$content = ['*'])
	{
		return $this->model->query()->where($where)->firstOrFail($content);
	}
	
	
	/**
	 * 查询一条数据是否存在
	 *
	 * @param $map
	 *
	 * @return bool 是否存在
	 */
	public  function be ( $map )
	{
		
		if ( ! is_array ( $map ) && empty( $field ) )
			$field = $this->model->getKeyName ();
		$map = ! is_array ( $map ) ? [ $field => $map ] : $map;
		$count =   $this->model->query()->where ( $map )
			->count ();
		
		return $count > 0 ;
	}
	
	/**
	 * @param $Page
	 * @param $PageSize
	 * @param array $where
	 * @param array $row
	 * @return mixed
	 */
	public  function getList($Page,$PageSize,$where=[],$row = ['*'])
	{
		
		
		$offset = ($Page- 1) * $PageSize;
		$data['list'] = $this->model->query()->where($where)->offset($offset)->limit($PageSize)->get ($row)->toArray ();
		
		$sum = $this->model->query()->where($where)->count ('id');
		
		$data['total'] = ceil ( $sum / $PageSize );
		$data['sum'] = $sum;
		
		
		return $data;
	}
	
	/**
	 * @param int $page
	 * @param array $where
	 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
	 */
	public function getPaginate ($page=15,$where = [])
	{
		return $this->model->query()->where ($where)->paginate ($page);
	}
	
	/**
	 * @param $map
	 * @param $data
	 * @return int
	 */
	public function updateData($map,$data)
	{
		return $this->model->query()->where($map)->update($data);
	}
	
	/**
	 * @param $map
	 * @return mixed
	 */
	public function delete($map)
	{
		return $this->model->query()->where($map)->delete();
	}
	
	
	/**
	 * @param $map
	 * @param array $data
	 * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
	 */
	public function getAll($map,$data=['*'])
	{
		return $this->model->query()->where($map)->get($data);
	}
	
	/**
	 * 添加一条数据
	 *
	 * @param $data
	 *
	 * @return Model $model 数据对象
	 */
	public  function set ( $data ): Model
	{
		return $this->model->query()->create ( $data );
	}
	
	/**
	 * @return Model
	 */
	public function getModel () : Model
	{
		return $this->model;
	}
	
}